package com.fingard.dsp.bank.directbank.jrtt01;

import java.io.*;
import java.util.*;

import com.fingard.FGBiz;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IForeignCurrCheck;
import com.fingard.dsp.bank.reqrespobj.frgcurrsinglecheck.ReqCJFrgCurrCheck;
import com.fingard.dsp.bank.reqrespobj.frgcurrsinglecheck.RetAtsCJFrgCurrCheck;
import com.fingard.dsp.bank.reqrespobj.frgcurrsinglecheck.detail.CJFrgCurrCheckDetail;
import com.fingard.dsp.bank.reqrespobj.frgcurrsinglecheck.detail.RetAtsCJFrgCurrCheckDetail;
import com.fingard.dsp.bank.reqrespobj.frgcurrsinglegetval.ReqCJFrgCurrGetVal;
import com.fingard.dsp.bank.reqrespobj.frgcurrsinglegetval.RetAtsCJFrgCurrGetVal;
import com.fingard.dsp.bank.reqrespobj.frgcurrsinglegetval.detail.*;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 财经-海外接口
 */
public class JRTT01ForeignCurrCheck extends JRTT01Base implements IForeignCurrCheck {

    /**
     * 付款信息校验
     * @param reqCheckSingleBat
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsCJFrgCurrCheck checkFrgCurrPayValue(ReqCJFrgCurrCheck reqCheckSingleBat) throws Exception {
        RetAtsCJFrgCurrCheck retAts = new RetAtsCJFrgCurrCheck(reqCheckSingleBat.reqHeader);
        try{
            //非公有参数
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            //拼接请求报文头
            JRTT01Header header=new JRTT01Header();
            header.requestType="verify.001.001.03";//交易码
            header.msgId=getQryTransID();
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeEndElement();
            //拼接请求报文体
            xmlTextWriter.writeStartElement("ReqDetail");
            int count=0;//统计数量
            while (reqCheckSingleBat.readRecord()) {
                CJFrgCurrCheckDetail tmpDetail = reqCheckSingleBat.readedDetail;
                xmlTextWriter.writeStartElement("PaymentInfo");
                xmlTextWriter.writeElementStringWithoutTransfer("Id",tmpDetail.askNumber);//批内流水
                xmlTextWriter.writeStartElement("Fields");
                xmlTextWriter.writeStartElement("Field");
                xmlTextWriter.writeElementStringWithoutTransfer("Name","TemplateName");//模板名称
                xmlTextWriter.writeElementStringWithoutTransfer("Value",tmpDetail.templateName);//模板名称
                xmlTextWriter.writeEndElement();
                if(StringHelper.hasAnyChar(tmpDetail.name) && StringHelper.hasAnyChar(tmpDetail.value)){
                    String[] flag=xmlTextWriterBody(xmlTextWriter,tmpDetail.name,tmpDetail.value,tmpDetail.fieldNum);
                    if(!"0".equals(flag[0])){
                        retAts.batRespCode = "-1";
                        retAts.batRespInfo = flag[1];
                        WriteBankLogLn(flag[1]);
                        retAts.writeAfterDetail();
                        return retAts;
                    }
                }
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeEndElement();
                count++;
            }
            if(count!=Integer.parseInt(reqCheckSingleBat.recordNum)){
                retAts.batRespCode = "-1";
                retAts.batRespInfo = "DetailRecord总数据与RecordNum不一致";
                WriteBankLogLn("DetailRecord总数据与RecordNum不一致");
                retAts.writeAfterDetail();
                return retAts;
            }
            xmlTextWriter.writeEndElement();
            xmlTextWriter.writeEndDocument();
            //发送请求报文
            String[] tmpStrRet = sendToBank(xmlTextWriter.toXmlString(),"rulevalidator");

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/Document/Response/GrpHdr/ResponseStatus").trim();//返回码
                    String returnMsg = tmpBkReader.getFirstText("/Document/Response/GrpHdr/AddtlInf");//返回信息
                    retAts.batRespCode = "0";
                    retAts.batRespInfo = returnMsg;
                    retAts.reqBatchNO = reqCheckSingleBat.reqBatchNO;
                    retAts.batTransState = returnCode;
                    retAts.batPayInfoCode = retAts.batTransState;
                    while (tmpBkReader.read("/Document/Response/Rpt/PaymentInfo")){
                        RetAtsCJFrgCurrCheckDetail retAtsCheckDetail=new RetAtsCJFrgCurrCheckDetail();
                        String transState=tmpBkReader.readedXElement.getSingleTextNotNull("PaymentStatus");
                        retAtsCheckDetail.reqSeqID=tmpBkReader.readedXElement.getSingleTextNotNull("Id");
                        retAtsCheckDetail.respCode="0";
                        retAtsCheckDetail.transState=transState;
                        retAtsCheckDetail.payInfoCode=retAtsCheckDetail.transState;
                        retAtsCheckDetail.respInfo=tmpBkReader.readedXElement.getSingleTextNotNull("Msg");
                        if("ERRO".equals(transState)){
                            if(tmpBkReader.readedXElement.getSingleChild("Fields")!=null){
                                List<XElement> xmlTextWriters=tmpBkReader.readedXElement.getSingleChild("Fields").getChildList("Field");
                                StringBuilder sbd=new StringBuilder(tmpBkReader.readedXElement.getSingleTextNotNull("Msg")).append(" ");
                                if(xmlTextWriters!=null && xmlTextWriters.size()>0){
                                    for(XElement xElement:xmlTextWriters){
                                        sbd.append(xElement.getSingleTextNotNull("Msg")).append(",");
                                    }
                                }
                                retAtsCheckDetail.respInfo=sbd.deleteCharAt(sbd.length()-1).toString();
                            }
                        }
                        retAts.writeRetDetail(retAtsCheckDetail);
                    }
                } else {
                    retAts.batRespCode = "-1";
                    retAts.batRespInfo = tmpSbMsg.toString();
                }
            }else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                retAts.batRespCode = "0";
                retAts.batRespInfo = tmpStrRet[1];
                retAts.reqBatchNO = reqCheckSingleBat.reqBatchNO;
                retAts.batTransState = PaymentState.FAIL_Payment;
                retAts.batPayInfoCode = tmpStrRet[0];
            } else {
                retAts.batRespCode = "-1";
                retAts.batRespInfo = tmpStrRet[1];
            }
        }catch (Exception e){
            e.printStackTrace();
            retAts.batRespCode = "-1";
            retAts.batRespInfo = e.getMessage();
            WriteBankLogLn("付款信息校验异常："+e);
        }
        retAts.writeAfterDetail();
        return retAts;
    }

    /**
     * 获取付款元素
     * @param reqFrgCurrSingle
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsCJFrgCurrGetVal getFrgCurrPayValue(ReqCJFrgCurrGetVal reqFrgCurrSingle) throws Exception {
        RetAtsCJFrgCurrGetVal retAts = new RetAtsCJFrgCurrGetVal(reqFrgCurrSingle.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo ="";
        try{
            WriteBankLogLn("开始获取付款元素，请求获取版本号为【"+reqFrgCurrSingle.version+"】");
            List<FrgCurrSingleBasic> currSingleOneList=new ArrayList<FrgCurrSingleBasic>();
            List<FrgCurrSingleBasic> currSingleList=new ArrayList<FrgCurrSingleBasic>();//存放响应的关键词，便于接口二请求
            //版本号数据存放目录
            String versionFilePath=FGBiz.dspCenter.dspSet.dataDirPath + File.separator + "dataFile" + File.separator + bankFront.code + File.separator +"versionFile";
            if(!FileHelper.existFile(versionFilePath)){
                FileHelper.mkdir(versionFilePath);
            }
            String ruleVersion="";//版本号
            FrgCurrSingleBasic currSingleOne=null;
            //======接口一 payment_rule======
            XmlTextWriter xmlTextWriter3 = new XmlTextWriter();
            //拼接请求报文头
            JRTT01Header header3=new JRTT01Header();
            header3.requestType="rule.060.001.03";//交易码
            header3.msgId=getQryTransID();
            writeCommonHeader(xmlTextWriter3,header3);
            xmlTextWriter3.writeEndElement();
            xmlTextWriter3.writeEndDocument();
            //发送请求报文
            String[] bankResult3 = sendToBank(xmlTextWriter3.toXmlString(),"rulevalidator");

            //处理响应报文
            if (bankResult3[0].length() == 0) {
                StringBuilder tmpSb = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult3[1], tmpSb);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/Document/Response/GrpHdr/ResponseStatus").trim();//返回码
                    String returnMsg = tmpBkReader.getFirstText("/Document/Response/GrpHdr/AddtlInf");//返回信息
                    if ("ACCP".equals(returnCode)) {
                        ruleVersion=tmpBkReader.getFirstText("/Document/Response/Rpt/RuleVersion");//版本号
                        WriteBankLogLn("银行响应版本号为【"+ruleVersion+"】");
                        if(StringHelper.isNullOrEmpty(ruleVersion)){
                            tmpRespCode = "-1";
                            tmpRespInfo = "响应版本号为空";
                        }
                    }else{
                        tmpRespCode = "-1";
                        tmpRespInfo = returnMsg;
                    }
                }else{
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSb.toString();
                }
            }else{
                tmpRespCode = "-1";
                tmpRespInfo = bankResult3[1];
            }
            if(!"-1".equals(tmpRespCode)){
                WriteBankLogLn("查询的版本号为【"+ruleVersion+"】");
                if(StringHelper.hasAnyChar(reqFrgCurrSingle.version) && Long.parseLong(ruleVersion)<=Long.parseLong(reqFrgCurrSingle.version)){
                    String versionFilePath2 = versionFilePath+File.separator+reqFrgCurrSingle.version+".txt";
                    if(FileHelper.existFile(versionFilePath2)){
                        WriteBankLogLn("此版本号在本地存在，开始获取版本号【"+reqFrgCurrSingle.version+"】，目录【"+versionFilePath2+"】存放的数据");
                        //获取此版本存放的数据
                        String rspXml=FileHelper.readFileText(versionFilePath2,getCharset());
                        if(StringHelper.hasAnyChar(rspXml)){
                            String[] rspXmlArr=rspXml.split("\r\n-\r\n");
                            for(String rsp:rspXmlArr){
                                if(StringHelper.hasAnyChar(rsp)){
                                    String[] respArr=new String[]{tmpRespCode,tmpRespInfo};
                                    //处理数据
                                    dealWithRspXml(rsp,retAts,respArr,reqFrgCurrSingle.version);
                                    tmpRespCode=respArr[0];
                                    tmpRespInfo=respArr[1];
                                }
                            }
                            if(!"-1".equals(tmpRespCode) && retAts.getWrittenRdCount()>0){
                                retAts.writeEnd();
                                return retAts;
                            }else {
                                retAts = new RetAtsCJFrgCurrGetVal(reqFrgCurrSingle.reqHeader);
                                retAts.writeStart();//写返回ATS的统一头部信息
                                tmpRespCode = "";
                                tmpRespInfo ="";
                            }
                        }
                    }else {
                        WriteBankLogLn("此版本号在本地不存在，开始向银行发起接口请求");
                    }
                }
            }
            if(!"-1".equals(tmpRespCode)) {
                //======接口二 payment_basic======
                //非公有参数
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                //拼接请求报文头
                JRTT01Header header = new JRTT01Header();
                header.requestType = "basic.060.001.03";//交易码
                header.msgId = getQryTransID();
                writeCommonHeader(xmlTextWriter, header);
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeEndDocument();
                //发送请求报文
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString(),"rulevalidator");

                //处理响应报文
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSb = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSb);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/Document/Response/GrpHdr/ResponseStatus").trim();//返回码
                        String returnMsg = tmpBkReader.getFirstText("/Document/Response/GrpHdr/AddtlInf");//返回信息
                        if ("ACCP".equals(returnCode)) {
                            while (tmpBkReader.read("/Document/Response/Rpt")) {
                                currSingleOne = new FrgCurrSingleBasic();
                                currSingleOne.bankType = tmpBkReader.readedXElement.getSingleTextNotNull("BankType");
                                List<XElement> payBankCountry = tmpBkReader.readedXElement.getChildList("PayBankCountry");
                                if (payBankCountry != null && payBankCountry.size() > 0) {
                                    for (XElement xElement : payBankCountry) {
                                        currSingleOne = new FrgCurrSingleBasic();
                                        currSingleOne.bankType = tmpBkReader.readedXElement.getSingleTextNotNull("BankType");
                                        currSingleOne.payBankCountryCode = xElement.getSingleTextNotNull("PayBankCountryCode");
                                        List<XElement> payMethod = xElement.getChildList("PayMethod");
                                        if (payMethod != null && payMethod.size() > 0) {
                                            for (XElement xElement2 : payMethod) {
                                                currSingleOne = new FrgCurrSingleBasic();
                                                currSingleOne.bankType = tmpBkReader.readedXElement.getSingleTextNotNull("BankType");
                                                currSingleOne.payBankCountryCode = xElement.getSingleTextNotNull("PayBankCountryCode");
                                                currSingleOne.methodName = xElement2.getSingleTextNotNull("MethodName");
                                                List<XElement> countryCurrencyGroup = xElement2.getChildList("CountryCurrencyGroup");
                                                if (countryCurrencyGroup != null && countryCurrencyGroup.size() > 0) {
                                                    for (XElement xElement3 : countryCurrencyGroup) {
                                                        currSingleOne = new FrgCurrSingleBasic();
                                                        currSingleOne.bankType = tmpBkReader.readedXElement.getSingleTextNotNull("BankType");
                                                        currSingleOne.payBankCountryCode = xElement.getSingleTextNotNull("PayBankCountryCode");
                                                        currSingleOne.methodName = xElement2.getSingleTextNotNull("MethodName");
                                                        currSingleOne.payCurrency = xElement3.getSingleTextNotNull("PayCurrency");
                                                        currSingleOne.beneBankCountry = xElement3.getSingleTextNotNull("BeneBankCountry");
                                                        currSingleOneList.add(currSingleOne);
                                                    }
                                                } else {
                                                    tmpRespCode = "-1";
                                                    tmpRespInfo = "字段缺失";
                                                }
                                            }
                                        } else {
                                            tmpRespCode = "-1";
                                            tmpRespInfo = "字段缺失";
                                        }
                                    }
                                } else {
                                    tmpRespCode = "-1";
                                    tmpRespInfo = "字段缺失";
                                }
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = returnMsg;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpBkReader.toString();
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = bankResult[1];
                }
            }
            //拆分币种和国家字段
            if(!"-1".equals(tmpRespCode) && currSingleOneList.size()>0){
                for(FrgCurrSingleBasic frgCurrSingleBasic:currSingleOneList){
                    String[] payCurrencyAry=frgCurrSingleBasic.payCurrency.split(",");
                    String[] beneBankCountryAry=frgCurrSingleBasic.beneBankCountry.split(",");
                    for(String payCurrency:payCurrencyAry){
                        for(String beneBankCountry:beneBankCountryAry){
                            currSingleOne=new FrgCurrSingleBasic();
                            currSingleOne.bankType=frgCurrSingleBasic.bankType;
                            currSingleOne.payBankCountryCode=frgCurrSingleBasic.payBankCountryCode;
                            currSingleOne.methodName=frgCurrSingleBasic.methodName;
                            currSingleOne.payCurrency=payCurrency;
                            currSingleOne.beneBankCountry=beneBankCountry;
                            currSingleList.add(currSingleOne);
                        }
                    }
                }
            }
            //校验银行响应币种是否都维护
            /*if(!"-1".equals(tmpRespCode) && currSingleList.size()>0){
                StringBuilder payCurrencySbd=new StringBuilder();
                for(FrgCurrSingleBasic frgCurrSingleBasic:currSingleList){
                    if(!"".equals(frgCurrSingleBasic.payCurrency) && !"*".equals(frgCurrSingleBasic.payCurrency)){
                        String payCurrency=convertCurToATS(frgCurrSingleBasic.payCurrency);
                        if(StringHelper.isNullOrEmpty(payCurrency)){
                            payCurrencySbd.append(frgCurrSingleBasic.payCurrency).append(",");
                        }
                    }
                }
                if(StringHelper.hasAnyChar(payCurrencySbd.toString())){
                    tmpRespCode = "-1";
                    tmpRespInfo = "银行响应基本付款信息中存在币种【"+payCurrencySbd.toString()+"】未配置，请检查配置文件";
                }
            }*/
            //======接口三 payment_detail======
            if(!"-1".equals(tmpRespCode) && currSingleList.size()>0){
                //开始获取详细付款信息
                for(FrgCurrSingleBasic frgCurrSingleBasic:currSingleList){
                    WriteBankLogLn("请求参数："+frgCurrSingleBasic.bankType+"|"+frgCurrSingleBasic.payBankCountryCode+"|"+frgCurrSingleBasic.methodName +"|"
                            +frgCurrSingleBasic.payCurrency+"|"+frgCurrSingleBasic.beneBankCountry+"|");
                    //非公有参数
                    XmlTextWriter xmlTextWriter2 = new XmlTextWriter();
                    //拼接请求报文头
                    JRTT01Header header2=new JRTT01Header();
                    header2.requestType="detail.060.001.03";//交易码
                    header2.msgId=getQryTransID();
                    writeCommonHeader(xmlTextWriter2,header2);
                    xmlTextWriter2.writeEndElement();
                    xmlTextWriter2.writeStartElement("ReqDetail");
                    xmlTextWriter2.writeElementStringWithoutTransfer("BankType",frgCurrSingleBasic.bankType);
                    xmlTextWriter2.writeElementStringWithoutTransfer("PayBankCountryCode",frgCurrSingleBasic.payBankCountryCode);
                    xmlTextWriter2.writeElementStringWithoutTransfer("MethodName",frgCurrSingleBasic.methodName);
                    xmlTextWriter2.writeElementStringWithoutTransfer("PayCurrency",frgCurrSingleBasic.payCurrency);
                    xmlTextWriter2.writeElementStringWithoutTransfer("BeneBankCountry",frgCurrSingleBasic.beneBankCountry);
                    xmlTextWriter2.writeEndElement();
                    xmlTextWriter2.writeEndDocument();

                    String[] bankResult2 = sendToBank(xmlTextWriter2.toXmlString(),"rulevalidator");

                    //处理响应报文
                    if (bankResult2[0].length() == 0) {
                        String[] respArr=new String[]{tmpRespCode,tmpRespInfo};
                        dealWithRspXml(bankResult2[1],retAts,respArr,ruleVersion);
                        tmpRespCode=respArr[0];
                        tmpRespInfo=respArr[1];
                        //记录此版本号的元素信息
                        if(!"-1".equals(tmpRespCode) && retAts.getWrittenRdCount()>0){
                            String saveVsionFilePath=versionFilePath+File.separator+ruleVersion+".txt";
                            FileHelper.writeFile(saveVsionFilePath,bankResult2[1]+"\r\n-\r\n",getCharset());
                            WriteBankLogLn2("写入文件：" + saveVsionFilePath + ",文件内容 \r\n");
                        }
                    }else {
                        tmpRespCode = "-1";
                        tmpRespInfo = bankResult2[1];
                    }
                }
            }
        }catch (Exception ex){
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            ex.printStackTrace();
            WriteBankLogLn("查询付款可用元素异常:"+ex);
        }
        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAtsCJFrgCurrGetVal(reqFrgCurrSingle.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        }else if(retAts.getWrittenRdCount() == 0) {
            retAts.writeRespInfo("交易为空");
            retAts.writeRespCode("0");
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 处理银行响应的报文--获取元素
     * @param rspXml 响应报文
     * @param retAts 响应到ats
     * @param respArr 状态信息
     * @throws Exception
     */
    private void dealWithRspXml(String rspXml,RetAtsCJFrgCurrGetVal retAts,String[] respArr,String version) throws Exception{
        StringBuilder tmpSb = new StringBuilder();
        XmlTextReader tmpBkReader = loadBankRespXmlReader(rspXml, tmpSb);
        if (tmpBkReader != null) {
            String returnCode = tmpBkReader.getFirstText("/Document/Response/GrpHdr/ResponseStatus").trim();//返回码
            String returnMsg = tmpBkReader.getFirstText("/Document/Response/GrpHdr/AddtlInf");//返回信息
            if ("ACCP".equals(returnCode)) {
                RetAtsCJFrgCurrGetValDetail detail=new RetAtsCJFrgCurrGetValDetail();
                detail.bankType=tmpBkReader.getFirstText("/Document/Response/ReqDetail/BankType");//银行类型
                detail.payBankCountryCode=tmpBkReader.getFirstText("/Document/Response/ReqDetail/PayBankCountryCode");//ISO3166 2位国家代码
                detail.methodName=tmpBkReader.getFirstText("/Document/Response/ReqDetail/MethodName");//付款方式
                detail.payCurrency=tmpBkReader.getFirstText("/Document/Response/ReqDetail/PayCurrency");
                /*if("".equals(payCurrency) || "*".equals(payCurrency)){
                    detail.payCurrency=payCurrency;
                }else {
                    detail.payCurrency=convertCurToATS(payCurrency);
                    if(StringHelper.isNullOrEmpty(detail.payCurrency)){
                        throw new Exception("银行响应币种【"+payCurrency+"】没有关联映射，请配置");
                    }
                }*/
                detail.beneBankCountry=tmpBkReader.getFirstText("/Document/Response/ReqDetail/BeneBankCountry");//ISO3166 2位国家代码
                detail.version=version;
                List<FrgCurrSingleTemplate> frgCurrSingleTemplateList=new ArrayList<FrgCurrSingleTemplate>();
                while (tmpBkReader.read("/Document/Response/Rpt/Template")){
                    FrgCurrSingleTemplate template=new FrgCurrSingleTemplate();
                    template.templateName=tmpBkReader.readedXElement.getSingleTextNotNull("TemplateName");
                    template.templateDescZh=tmpBkReader.readedXElement.getSingleTextNotNull("TemplateDescZh");
                    template.templateDescEn=tmpBkReader.readedXElement.getSingleTextNotNull("TemplateDescEn");
                    List<XElement> xElementList=tmpBkReader.readedXElement.getSingleChild("Rule").getChildList("Field");
                    List<FrgCurrSingleField> frgCurrSingleFieldList=new ArrayList<FrgCurrSingleField>();
                    if(xElementList!=null && xElementList.size()>0){
                        for(XElement xElement:xElementList){
                            FrgCurrSingleField frgCurrSingleField=new FrgCurrSingleField();
                            frgCurrSingleField.name=xElement.getSingleTextNotNull("Name");//字段名
                            frgCurrSingleField.reqiured=typeConversion(xElement.getSingleTextNotNull("Reqiured"));//mandatory-必输，supress-不输入，optional-选输
                            //枚举取值
                            String enums=xElement.getSingleTextNotNull("Enums").trim();//枚举值
                            if(enums.contains(flag)){
                                List<XElement> xElementList1=tmpBkReader.readedXElement.getSingleChild(enums.replace(flag,"")).getChildList("Code");
                                if(xElementList1!=null && xElementList1.size()>0){
                                    StringBuilder sb=new StringBuilder();
                                    for(XElement xElement1:xElementList1){
                                        if(StringHelper.hasAnyChar(xElement1.getSingleTextNotNull("Name"))){
                                            sb.append(xElement1.getSingleTextNotNull("Name"));
                                        }
                                        if(StringHelper.hasAnyChar(xElement1.getSingleTextNotNull("Desc"))){
                                            sb.append("-||-").append(xElement1.getSingleTextNotNull("Desc")).append("@@");
                                        }
                                        if("true".equals(xElement1.getSingleTextNotNull("IsDefault"))){
                                            frgCurrSingleField.Default=xElement1.getSingleTextNotNull("Name");
                                        }
                                    }
                                    frgCurrSingleField.checkRule="5";
                                    if(sb.length()>2){
                                        frgCurrSingleField.enums=sb.deleteCharAt(sb.length() - 2).toString();//以,分割多个可能的枚举值
                                    }
                                }
                            }else {
                                frgCurrSingleField.enums=enums.replaceAll(",","@@");//以,分割多个可能的枚举值
                            }
                            //checkRule、type处理方式
                            String checkRule="";
                            String type="0";
                            //取第一行的rule
                            XElement ruleList=xElement.getSingleChild("CheckRules").getSingleChildByIndex("Rule",0);
                            String rule=ruleList.innerText;
                            String msgEn=ruleList.hashMapAttributes.get("msgEn");
                            String msgZh=ruleList.hashMapAttributes.get("msgZh");
                            if(StringHelper.hasAnyChar(msgEn)){
                                if(msgEn.contains("date")){
                                    type="2";
                                    checkRule="10";
                                }else if (msgEn.contains("amount")){
                                    type="1";
                                    checkRule="";
                                }else {
                                    type="0";
                                    checkRule=getMaxLength(msgZh);
                                }
                            }
                            frgCurrSingleField.checkRule=checkRule;//
                            frgCurrSingleField.type=type;//字段类型
                            frgCurrSingleField.regex=StringHelper.hasAnyChar(rule)?"<![CDATA["+rule+"]]":rule;//正则表达式
                            //charSetCheck处理方式
                            String charSetCheck=xElement.getSingleTextNotNull("CharSetCheck").trim();
                            if("true".equals(charSetCheck)){
                                frgCurrSingleField.charSetCheck="<![CDATA["+tmpBkReader.readedXElement.getSingleTextNotNull("CharSet")+"]]>";
                            }else {
                                frgCurrSingleField.charSetCheck="";//字符集
                            }
                            frgCurrSingleFieldList.add(frgCurrSingleField);
                        }
                    }
                    template.field=frgCurrSingleFieldList;
                    frgCurrSingleTemplateList.add(template);
                }
                detail.template=frgCurrSingleTemplateList;
                retAts.writeRetDetail(detail);
            }else if("ERRO".equals(returnCode)){
                RetAtsCJFrgCurrGetValDetail detail=new RetAtsCJFrgCurrGetValDetail();
                detail.bankType=tmpBkReader.getFirstText("/Document/Response/ReqDetail/BankType");//银行类型
                detail.payBankCountryCode=tmpBkReader.getFirstText("/Document/Response/ReqDetail/PayBankCountryCode");//ISO3166 2位国家代码
                detail.methodName=tmpBkReader.getFirstText("/Document/Response/ReqDetail/MethodName");//付款方式
                detail.payCurrency=tmpBkReader.getFirstText("/Document/Response/ReqDetail/PayCurrency");//ISO4217 3位币种代码
                detail.beneBankCountry=tmpBkReader.getFirstText("/Document/Response/ReqDetail/BeneBankCountry");//ISO3166 2位国家代码
                detail.version=version;
                detail.template=null;
                retAts.writeRetDetail(detail);
            }else {
                respArr[0] = "-1";
                respArr[1] = returnMsg;
            }
        }else {
            respArr[0] = "-1";
            respArr[1] = tmpBkReader.toString();
        }
    }
}
